using System;

namespace SharedKernel.Domain
{
    /// <summary>
    /// 包含创建、修改属性的实体基类
    /// </summary>
    /// <typeparam name="TId">实体ID类型</typeparam>
    public abstract class CreateUpdateEntity<TId> : CreateEntity<TId>, IEquatable<CreateUpdateEntity<TId>> where TId : IEquatable<TId>
    {
        /// <summary>
        /// 修改人id
        /// </summary>
        public TId UpdatedUserId { get; protected set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdateTime { get; protected set; }
         

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">实体ID</param>
        protected CreateUpdateEntity(TId id) : base(id)
        {
            Id = id; 
        }


        /// <summary>
        /// 相等性比较
        /// </summary>
        public bool Equals(CreateUpdateEntity<TId> other)
        {
            if (other is null)
                return false;

            return Id.Equals(other.Id);
        }

        /// <summary>
        /// 相等性比较
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj is null)
                return false;

            if (obj.GetType() != GetType())
                return false;

            if (obj is not CreateUpdateEntity<TId> entity)
                return false;

            return Id.Equals(entity.Id);
        }

        /// <summary>
        /// 获取哈希码
        /// </summary>
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        /// <summary>
        /// 相等运算符
        /// </summary>
        public static bool operator ==(CreateUpdateEntity<TId> left, CreateUpdateEntity<TId> right)
        {
            if (left is null && right is null)
                return true;

            if (left is null || right is null)
                return false;

            return left.Equals(right);
        }

        /// <summary>
        /// 不等运算符
        /// </summary>
        public static bool operator !=(CreateUpdateEntity<TId> left, CreateUpdateEntity<TId> right)
        {
            return !(left == right);
        }
    }
}